<template>
  <div>
    <el-card header="表单配置">
      <el-form ref="formEdit" :model="node" label-width="200px">
        <el-form-item label="审核表单" prop="FormId">
          <formInput v-model="node.FormId" :readonly="readonly" placeholder="选择表单" prt-form-classify="02" :form-type="[0,1]" />
        </el-form-item>
        <el-form-item label="是否允许编辑" prop="IsAllowEdit">
          <el-switch
            v-model="node.IsAllowEdit"
            :disabled="readonly"
          />
        </el-form-item>
      </el-form>
      <div v-if="readonly == false" style="text-align: center;margin-top: 10px;">
        <el-button type="primary" @click="saveForm">保存</el-button>
        <el-button @click="reset">重置</el-button>
      </div>
    </el-card>
    <el-card v-if="curForm != defFormId && curForm" style="margin-top: 10px;">
      <div slot="header">
        <span>数据传递</span>
      </div>
      <w-table
        :data="powers"
        :columns="columns"
        :is-paging="false"
        :is-show-no="true"
      >
        <template slot="colId" slot-scope="e">
          <formColChiose v-model="e.row.ColId" :cur-form-id.sync="e.row.FormId" :form-id="curFormId" />
        </template>
        <template slot="keywords" slot-scope="e">
          <el-input v-model="e.row.Keywords" placeholder="筛选方案名" clearable />
        </template>
        <template slot="action" slot-scope="e">
          <el-button icon="el-icon-delete" size="mini" type="danger" circle @click="remove(e.row)" />
        </template>
      </w-table>
      <div v-if="readonly == false" style="text-align: center;margin-top: 10px;">
        <el-button type="primary" @click="savePower">保存</el-button>
        <el-button @click="load">重置</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { EnumDic } from '@/flow/config/flowConfig'
import * as flowNodeApi from '@/flow/api/flowNode'
import { GetFormId } from '@/flow/api/flow'
import formInput from '@/customForm/components/form/formInput.vue'
import * as relationApi from '@/flow/api/flowNodeRelation'
import formColChiose from '@/customForm/components/form/formColChiose.vue'
import { GetStructures } from '@/customForm/api/form'
export default {
  components: {
    formInput,
    formColChiose
  },
  props: {
    source: {
      type: Object,
      default: () => {
        return {
          FormId: null,
          IsAllowEdit: false
        }
      }
    },
    nodeId: {
      type: String,
      default: null
    },
    flowId: {
      type: String,
      default: null
    },
    defFormId: {
      type: String,
      default: null
    },
    readonly: {
      type: Boolean,
      default: false
    },
    isLoad: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EnumDic,
      node: {},
      powers: [],
      curFormId: [],
      nodeFormId: [],
      formIds: null,
      curForm: null,
      toForm: null,
      id: 1,
      columns: [{
        key: 'ColId',
        title: '来源表单字段',
        align: 'center',
        slotName: 'colId'
      }, {
        key: 'toLabel',
        title: '对应表单字段',
        align: 'left'
      }, {
        key: 'Keywords',
        title: '筛选方案名',
        align: 'center',
        slotName: 'keywords',
        with: '120px'
      }]
    }
  },
  watch: {
    isLoad: {
      handler(val) {
        if (val) {
          this.reset()
          this.init(this.source.FormId)
        }
      },
      immediate: true
    },
    flowId: {
      handler(val) {
        if (val) {
          this.loadFormId()
        }
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    saveForm() {
      const that = this
      this.$refs['formEdit'].validate((valid) => {
        if (valid) {
          that.set()
        } else {
          return false
        }
      })
    },
    async savePower() {
      const list = this.powers.filter(c => c.ColId != null)
      await relationApi.Sync({
        FlowId: this.flowId,
        NodeId: this.nodeId,
        Relations: list.map(c => {
          return {
            ColId: c.ColId,
            FormId: c.FormId,
            ToColId: c.ToColId,
            Keywords: c.Keywords
          }
        })
      })
      this.$message({
        message: '保存成功!',
        type: 'success'
      })
    },
    async set() {
      const res = await flowNodeApi.SetForm(this.nodeId, this.node)
      this.$message({
        message: '更新成功!',
        type: 'success'
      })
      if (res) {
        this.curForm = this.node.FormId
        this.$emit('refresh', this.node)
        this.init(this.node.FormId)
      }
    },
    reset() {
      this.node = {
        FormId: this.source.FormId,
        IsAllowEdit: this.source.IsAllowEdit
      }
      this.curForm = this.source.FormId
    },
    init(formId) {
      if (formId != null) {
        this.nodeFormId = [formId]
        this.loadToForm()
      } else {
        this.nodeFormId = null
        this.toForm = null
        this.powers = []
      }
    },
    async loadFormId() {
      this.formIds = await GetFormId(this.flowId)
    },
    async loadToForm() {
      const res = await GetStructures({
        FormId: this.nodeFormId,
        IsSystem: false
      })
      this.toForm = res
      if (this.node.FormId != null) {
        this.curFormId = this.formIds.filter(c => c !== this.node.FormId)
      } else {
        this.curFormId = this.formIds.filter(c => c !== this.defFormId)
      }
      this.load()
    },
    loadCols(to, powers, list, label, formId) {
      if (to.Type === 4) {
        formId = to.Id
      }
      if (to.Children != null && to.Children.length > 0) {
        if (label == null) {
          label = to.Label
        } else {
          label = label + '/' + to.Label
        }
        to.Children.forEach(c => {
          this.loadCols(c, powers, list, label, formId)
        })
      } else if (to.Type === 2) {
        const t = powers.find(a => a.ToColId === to.Id)
        const data = {
          ColId: null,
          FormId: formId,
          ToColId: to.Id,
          toLabel: label + '/' + to.Label,
          Keywords: null
        }
        if (t != null) {
          data.ColId = t.ColId
          data.Keywords = t.Keywords
        }
        list.push(data)
      }
    },
    async load() {
      let res = await relationApi.Gets(this.nodeId)
      const list = []
      if (res == null) {
        res = []
      }
      this.toForm.forEach(c => {
        this.loadCols(c, res, list, null, null)
      })
      this.powers = list
    }
  }
}
</script>

